import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {TOKENURL} from "../../config/url.config";
import {Observable} from "rxjs/Observable";

@Injectable()
export class LoginProvider {

    constructor(public http: HttpClient) {
    }

    doLogin(params: any): Observable<any> {

        let url = TOKENURL;
        let body = `grant_type=password&username=${params.UserName}&password=${params.Password}`;
        let options = {headers: new HttpHeaders().set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")};

        return this.http.post(url, body, options)
    }

    setLocalStorage(res) {
        localStorage.setItem('hasSubordinate', res.hasSubordinate);
        localStorage.setItem('access_token', res.access_token);
        localStorage.setItem('userName', res.userName);
    }

}
